package com.ymttest.database.sqlwapper.sqdistributor;

import java.util.HashMap;
import java.util.List;

import com.ymt.core.tool.Logger;
import com.ymt.database.SqlManager;
import com.ymttest.database.model.sqdistributor.DistributorRelation;

public class DistributorRelationWrapper {
	
	SqlManager manager;

	public DistributorRelationWrapper() {
		manager = new SqlManager("sqdistributor");
	}

	@SuppressWarnings("unchecked")
	public DistributorRelation selectByPrimaryKey(Long id) {

		DistributorRelation distributorRelation = null;
		List<DistributorRelation> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.sqdistributor.DistributorRelationMapper.selectByPrimaryKey",
						id);
		manager.commit();

		if (result.size() > 0) {
			distributorRelation = result.get(0);
		}

		return distributorRelation;
	}
	
	@SuppressWarnings("unchecked")
	public List<DistributorRelation> selectByMerchantId(Long merchantId) {

		List<DistributorRelation> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.sqdistributor.DistributorRelationMapper.selectByMerchantId",
						merchantId);
		manager.commit();

		return result;
	}
	
	@SuppressWarnings("unchecked")
	public List<DistributorRelation> selectByMerchantIdAndLevel(Long merchantId, Integer level) {
		
		HashMap<String, Object> map = new HashMap<>();
		
		map.put("merchantId", merchantId);
		map.put("level", level);
		
		List<DistributorRelation> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.sqdistributor.DistributorRelationMapper.selectByMerchantIdAndLevel",
						map);
		manager.commit();

		return result;
	}
	
	@SuppressWarnings("unchecked")
	public DistributorRelation selectByDistributorId(Long distributorId) {

		DistributorRelation distributorRelation = null;
		List<DistributorRelation> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.sqdistributor.DistributorRelationMapper.selectByDistributorId",
						distributorId);
		manager.commit();

		if (result.size() > 0) {
			distributorRelation = result.get(0);
		}

		return distributorRelation;
	}
	
	@SuppressWarnings("unchecked")
	public DistributorRelation selectByMerchantIdAndDistributorId(Long merchantId, Long distributorId) {

		DistributorRelation distributorRelation = null;
		
		HashMap<String, Long> map = new HashMap<>();
		
		map.put("merchantId", merchantId);
		map.put("distributorId", distributorId);
		
		List<DistributorRelation> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.sqdistributor.DistributorRelationMapper.selectByMerchantIdAndDistributorId",
						map);
		manager.commit();

		if (result.size() > 0) {
			distributorRelation = result.get(0);
		}

		return distributorRelation;
	}
	
	@SuppressWarnings("unchecked")
	public List<DistributorRelation> selectByMerchantIdAndParentDistributorId(Long merchantId, Long parentDistributorId) {

		HashMap<String, Long> map = new HashMap<>();
		
		map.put("merchantId", merchantId);
		map.put("parentDistributorId", parentDistributorId);
		
		List<DistributorRelation> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.sqdistributor.DistributorRelationMapper.selectByMerchantIdAndParentDistributorId",
						map);
		manager.commit();

		return result;
	}
	
	@SuppressWarnings("unchecked")
	public List<DistributorRelation> selectByMerchantIdAndDistributorPhone(
			Long merchantId, String distributorPhone) {

		HashMap<String, Object> map = new HashMap<>();

		map.put("merchantId", merchantId);
		map.put("distributorPhone", distributorPhone);

		List<DistributorRelation> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.sqdistributor.DistributorRelationMapper.selectByMerchantIdAndDistributorPhone",
						map);
		manager.commit();

		if (result.size() == 0) {
			Logger.comment("distributor_relation表没有数据");
		}

		return result;
	}
	
	public void insert(DistributorRelation distributorRelation) {

		manager.insert(
				"com.ymttest.database.mapping.sqdistributor.DistributorRelationMapper.insertSelective",
				distributorRelation);
		manager.commit();
	}
	
	public void updateDistributorStatus(Long distributorId) {

		manager.update(
				"com.ymttest.database.mapping.sqdistributor.DistributorRelationMapper.updateDistributorStatus",
				distributorId);
		manager.commit();
	}
	
	public Integer deleteDistributorRelationByMerchantId(Long merchantId) {

		int result = manager.delete(
				"com.ymttest.database.mapping.sqdistributor.DistributorRelationMapper.deleteDistributorRelationByMerchantId",
				merchantId);
		manager.commit();
		
		return result;
	}
	
	@SuppressWarnings("unchecked")
	public List<DistributorRelation> selectAgencyListByMerchantId(Long merchantId) {
		
		List<DistributorRelation> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.sqdistributor.DistributorRelationMapper.selectAgencyListByMerchantId",
						merchantId);
		manager.commit();

		return result;
	}
	
	@SuppressWarnings("unchecked")
	public List<DistributorRelation> selectAgencyListByMerchantIdAndLevel(Long merchantId, Integer level) {
		
		HashMap<String, Object> map = new HashMap<>();
		
		map.put("merchantId", merchantId);
		map.put("level", level);
		
		List<DistributorRelation> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.sqdistributor.DistributorRelationMapper.selectAgencyListByMerchantIdAndLevel",
						map);
		manager.commit();

		return result;
	}
	
	@SuppressWarnings("unchecked")
	public List<DistributorRelation> selectDistributorListByParentIdAndType(Long parentId, Integer type) {
		
		HashMap<String, Object> map = new HashMap<>();

		map.put("parent_distributor_id", parentId);
		map.put("distributor_type", type == 1 ? 1001 : 2001);
		
		List<DistributorRelation> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.sqdistributor.DistributorRelationMapper.selectDistributorListByParentIdAndType",
						map);
		manager.commit();

		return result;
	}
	
	public static void main(String[] args) {
		
		DistributorRelationWrapper test = new DistributorRelationWrapper();
		
//		SellerLevelHistory sellerLevelHistory = new SellerLevelHistory();
//		
//		sellerLevelHistory.setDDate(YMTDateUtil.getDate());
//		sellerLevelHistory.setSellerID(24635724);
//		sellerLevelHistory.setUpdateTime(YMTDateUtil.getDate());
//		sellerLevelHistory.setUnconfirm_amount(BigDecimal.TEN);
//		
		System.out.println(test.selectDistributorListByParentIdAndType(1167266153L, 1).size());
	}
}
